from django.db import models
import datetime
from django.contrib.auth.hashers import make_password, check_password
# Create your models here.


class Department(models.Model):
    department_id = models.BigAutoField(primary_key=True)
    # BigAutoField自增大小自增Field域，自动增加的一个数据库字段类型
    title = models.CharField(verbose_name="部门名", max_length=100, null=True, blank=True, default="")
    # verbose_name设置模型字段或模型本身的人类可读名称（通常是中文)

    def __str__(self):
        return self.title


class UserInfo(models.Model):
    user_id = models.CharField(verbose_name="用户账号", primary_key=True, max_length=100, default="")
    # primary_key（主键）用于唯一标识一张表中的每一条记录。它必须是唯一的，不能为空，每条记录只能有一个主键值
    name = models.CharField(verbose_name="名字", max_length=30, null=True, blank=True, default="")
    password = models.CharField(verbose_name="密码", max_length=64, null=True, blank=True, default="")
    age = models.IntegerField(verbose_name="年龄", max_length=10, null=True, blank=True, default=18)
    account = models.DecimalField(verbose_name="余额", max_digits=10, decimal_places=2, default=0)
    # DecimalField	表示这是一个高精度的浮点数字段，适合用于金额 ,decimal_places=2 小数点后保留 2 位
    # max_digits=10 表示保留十位数字 包括小数部分
    create_time = models.DateTimeField(verbose_name="入职时间", default=datetime.datetime.now())
    gender_choice = ((1, '男'), (2, '女'))
    gender = models.SmallIntegerField(verbose_name="性别", choices=gender_choice, default=0)
    # SmallIntegerField小整数类型，适合存储枚举值（比 Integer 更省空间）
    # choices=gender_choices
    department = models.ForeignKey(to="Department", to_field="department_id", related_name="user",
                                   on_delete=models.CASCADE)
    # to=外键指向的模型类是 "Department",to_field 对应 Department 模型中的 department_id 字段，而不是默认主键 id
    # related_name 反向关联名称，即通过 Department 实例可以访问所有关联的用户 department.user_set.all()

    def __str__(self):
        return self.name


class PhoneNumbers(models.Model):
    mobile = models.IntegerField(verbose_name="手机号", max_length=11, null=True, blank=True, default=0)
    price = models.DecimalField(verbose_name="价格", max_digits=10, decimal_places=2, default=0, null=True, blank=True)
    level_choices = ((1, "一级"), (2, "二级"), (3, "三级"))
    level = models.SmallIntegerField(verbose_name="等级", choices=level_choices, default=1)
    status_choices = ((1, "占用"), (2, "未占用"))
    status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=1)

    def __str__(self):
        return self.mobile


class MyAdmin(models.Model):
    id = models.CharField(verbose_name="管理员账号", primary_key=True, max_length=32)
    user_name = models.CharField(verbose_name="管理员名称", max_length=32)
    password = models.CharField(verbose_name="管理员密码", max_length=64)

    class Meta:
        verbose_name = "管理员"
        db_table = "管理员表"
        # Meta用来配置模型的行为、显示、排序、表名等规则，不会直接创建数据库字段，但会影响模型的表现。
        # 其他还有ordering	默认排序字段等

    def __str__(self):
        return self.user_name


class Task(models.Model):
    title = models.CharField(verbose_name="任务标题", max_length=64)
    level_choices = ((1, "紧急"), (2, "一般"), (3, "不紧急"))
    level = models.SmallIntegerField(verbose_name="任务等级", choices=level_choices, default=3)
    user = models.ForeignKey(verbose_name="负责人", to="MyAdmin", to_field="id", null=True, on_delete=models.CASCADE)
    detail = models.TextField(verbose_name="任务详情", null=True, blank=True)
    # TextField 储存长文本


class Order(models.Model):
    order_id = models.CharField(verbose_name="订单号", primary_key=True, max_length=64)
    title = models.CharField(verbose_name="名称", max_length=32, blank=True, null=True, default="")
    price = models.DecimalField(verbose_name="价格", decimal_places=2, max_digits=10, default=0, null=True, blank=True)
    status_choice = ((1, "待支付"), (2, "支付中"), (3, "支付完成"))
    status = models.SmallIntegerField(verbose_name="支付状态", choices=status_choice, default=1)
    admin = models.ForeignKey(verbose_name="管理员", to="MyAdmin", to_field="id", null=True, on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name="用户", to="UserInfo", to_field="user_id", null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.title
